<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description of style
 *
 * @author kaito
 * 
 */
class style extends MY_Admin_Controller {

	public function __construct()
    {
        parent::__construct();
        $this->load->model('Style_model');
    }
    //put your code here
    public function index()
    {
        $pagesize = 10;
        
        $keyWords = $this->input->post('txtKey');
        $whereArr = array(
           'name like'=>"'%$keyWords%'", 
        );
        $arrParam['keywords'] = $keyWords;
        
        $page = _get_page();
        
        $list = $this->Style_model->fetch_page($page, 10, $whereArr, '*', ' id desc');
        
        //分页
        $pagecfg = array();
        $pagecfg['base_url'] = _create_url('admin/style', $arrParam);
        $pagecfg['total_rows']   = $list['count'];
        $pagecfg['cur_page'] = $page;
        $pagecfg['per_page'] = $pagesize;
        
        $this->pagination->initialize($pagecfg);
        $list['pages'] = $this->pagination->create_links();
        
        $data = array(
            'list' => $list,
            'style' => _get_config('searchclass')
        );
        $this->load->view('admin/style', $data);
    }
    
    /**
     * 增加
     */
    function add() 
    {
        if ($this->input->is_post()) {
			//验证规则
            $config = array(
                array(
                     'field'   => 'title', 
                     'label'   => '名称', 
                     'rules'   => 'trim|required'
                  ),
                array(
                    'field' => 'style',
                    'label' => '分类名称',
                    'rules' => 'trim|required'
                ),            
                            
                array(
                     'field'   => 'display', 
                     'label'   => '显示', 
                     'rules'   => 'trim|required'
                  ),  
            );

            $this->form_validation->set_rules($config);

            if ($this->form_validation->run() === TRUE) {
                $data = array(
                        'name'=>$this->input->post('title'),
                        'is_show'=>$this->input->post('display'),
                        's_style' => $this->input->post('style')
                );

                $id = _get_key_val($this->input->get('id'), TRUE);
                if ($id) {
                    $data['id'] = $id;
                }
                
                $this->Style_model->insert($data);

                //echo '成功,<a href="/admin/aa">返回列表页</a>';
                redirect(base_url('/admin/style'));
                exit;
            } 
        } else {
            $id	= _get_key_val($this->input->get('id'), TRUE);
            $result = array(
                'style' => _get_config('searchclass')
            );

            if(!empty($id))
            {
                $info = $this->Style_model->get_by_id($id);
                $result['info'] =$info;
            }
        } 
            
        $this->load->view('admin/style_add', $result);      
    }
    
    // 是否显示
    public function show()
    {
        $id = _get_key_val($this->input->get('id'), TRUE);

        if (!empty($id)) {
            $info = $this->Style_model->get_by_id($id);
            
            if ($info['is_show'] == '1') {
                $data['is_show'] = 0;
            } else {
                $data['is_show'] = 1;
            }
            
            $data['id'] = $id;
            $this->Style_model->insert($data);
        }
        
        redirect(base_url()."/admin/style");
    }
    
    // 删除
    public function del()
    {
        $id = _get_key_val($this->input->get('id'), true);
        
        if (!empty($id)) {
           $this->Style_model->delete_by_id($id);
        }
        
        redirect(base_url()."/admin/style/");
    }
    
}
